layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function() {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    var add_flag = false;

    //添加等级
    $(".batchAdd_btn").click(function(){

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['400px', '300px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_codebatch_win")
            ,end:function () {
                table.reload('paycode_batch_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });

    });

    table.render({
        elem: '#paycode_batch_table'
        ,url: '/mch_payCode/queryBatchData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id',width:200, title: '批次号',"align":"left"}
            ,{field:'batchCount',width:200, title: '数量',"align":"left"}
            ,{field:'createdTime',width:200, title: '创建时间',"align":"left"},
            ,{field:'status',width:200, title: '收款码下载',"align":"left",templet:function (d) {
                    return     '<a href="/mch_payCode/down?batchId='+d.id+'" class="layui-btn layui-btn-xs" >下载</a>';
            }}
        ]]
        ,request:{
            pageName: 'pageNo'
            ,limitName: 'pageSize'
        },
        where:{}
        ,id: 'paycode_batch_table'
        ,page: false
    });

    form.on('submit(addbatchOk)',function (data) {
        if (!add_flag){
            add_flag = true;
            $.ajax({
                url: "/mch_payCode/addBatchCode",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        add_flag = false;
                        layer.msg("批量创建成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('paycode_batch_table', {});
                                layer.closeAll();
                            }
                        })
                    } else {
                        add_flag = true;
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else{

        }
        return false;
    });

    form.verify({
        batchLimit: function (value) {

            if (value>100||value<1) {
                return '最大批量创建个数为100个';
            }
        },
    });
});